#include "Scene.h"

#include "SceneManager.h"

using namespace std;

//Constructor
Scene::Scene(std::string name)
{
	_name = name;
}



//Init Scene Objects
void Scene::Init()
{
	_active = true;

	_projection.Identity();
	_projection = Perspective(70, SceneManager::Instance()->GetWidth() / (float)SceneManager::Instance()->GetHeight(), 1, 10000);

	Landscape* _landscape = new Landscape();
	_landscape->SetParameters(33, 33, 20, 20, 1.0f);
	//_landscape->AddComponent(new Rigidbody());


	/*
	_go = new GameObject();
	_go->AddComponent(new Rigidbody());
	if(3 == _go->GetComponent<Rigidbody>()->GetTest())
		int t = 0;
	_go->GetTransform()->position.x = 5.0f;

	_go->AddComponent(new MeshFilter());
	MeshFilter* mesh = _go->GetComponent<MeshFilter>();
	*/

	/*
	GameObject* footLeft = new Cube("Foot left");
	footLeft->GetTransform()->scale.x = 2;
	footLeft->GetTransform()->scale.z = 4;

	footLeft->GetTransform()->position.x = -3.0f;
	footLeft->GetTransform()->position.y = 0.5f;
	footLeft->GetTransform()->position.z = 2.0f;

	GameObject* footRight = new Cube("Foot right");
	footRight->GetTransform()->scale.x = 2;
	footRight->GetTransform()->scale.z = 4;

	footRight->GetTransform()->position.x = 3.0f;
	footRight->GetTransform()->position.y = 0.5f;
	footRight->GetTransform()->position.z = 2.0f;

	GameObject* legLeft = new Cube("Leg right");
	legLeft->GetTransform()->scale.x = 2;
	legLeft->GetTransform()->scale.y = 3;
	legLeft->GetTransform()->scale.z = 2;

	legLeft->GetTransform()->position.x = -3.0f;
	legLeft->GetTransform()->position.y = 4.5f;

	GameObject* legRight = new Cube("Leg right");
	legRight->GetTransform()->scale.x = 2;
	legRight->GetTransform()->scale.y = 3;
	legRight->GetTransform()->scale.z = 2;

	legRight->GetTransform()->position.x = 3.0f;
	legRight->GetTransform()->position.y = 4.5f;


	GameObject* molletLeft = new Cube("Mollet left");
	molletLeft->GetTransform()->scale.x = 2;
	molletLeft->GetTransform()->scale.y = 4;
	molletLeft->GetTransform()->scale.z = 2;

	molletLeft->GetTransform()->position.x = -3.0f;
	molletLeft->GetTransform()->position.y = 11.5f;

	GameObject* molletRight = new Cube("Mollet right");
	molletRight->GetTransform()->scale.x = 2;
	molletRight->GetTransform()->scale.y = 4;
	molletRight->GetTransform()->scale.z = 2;

	molletRight->GetTransform()->position.x = 3.0f;
	molletRight->GetTransform()->position.y = 11.5f;

	GameObject* ankle = new Cube("Ankle");
	ankle->GetTransform()->scale.x = 5;
	ankle->GetTransform()->scale.y = 2;
	ankle->GetTransform()->scale.z = 2;

	ankle->GetTransform()->position.y = 17.5f;
	*/

}



//Update
void Scene::Update(float time)
{

	static float angle = 0;

	//Matrice	
	_modelview.Identity();
	//_projection.Identity();

	/*
	sdl_events();

	startTime = SDL_GetTicks();

		SDL_PollEvent(&m_events);

		if(m_events.window.event == SDL_WINDOWEVENT_CLOSE)
			exit = true;
			*/

		//Clean screen
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		//Reinitialise Matrice
		_modelview.Identity();
		_modelview = LookAt(Vector3(0, 10, -25), Vector3(0, 0, 0), Vector3(0, 1, 0));
		Matrix4 rotationCamera = RotateY(angle);

		_modelview = MultiplyMatrix(_modelview, rotationCamera);

		angle += 0.4f;

		if(angle >= 360.0)
			angle -= 360.0;


		//Rotate

		//Update & Render Game Objects
		GameObject::UpdateScene((float)time);
		GameObject::RenderScene(_modelview, _projection);

		//Update window
		SDL_GL_SwapWindow(SceneManager::Instance()->GetWindow());

	
}


//Destructor
Scene::~Scene()
{
	if(_active)
	{
		GameObject::DeleteScene();
	}

}